// utils/request.js

import axios from 'axios'
import { ElMessage } from 'element-plus'

export const baseBackURL = 'http://192.168.0.152:2000/apis/category'

// 创建axios实例
const service = axios.create({
    baseURL: baseBackURL,
    timeout: 60000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
    config => {
        // 在发送请求之前做些什么
        return config
    },
    error => {
        // 对请求错误做些什么
        console.log(error)
        return Promise.reject(error)
    }
)

// response 拦截器
service.interceptors.response.use(
    response => {
        const res = response.data

        // 如果返回的状态码不是200，则判断为错误
        if (response.status !== 200) {
            ElMessage({
                message: res.message || 'Error',
                type: 'error',
                duration: 5 * 1000
            })
            return Promise.reject(new Error(res.message || 'Error'))
        } else {
            return res
        }
    },
    error => {
        console.log('err' + error)
        ElMessage({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)

export default service